/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GastoSegunObjetoListar.java
 *
 * Created on 07/03/2011, 04:21:53 PM
 */
package org.gingresos.regimenimpositivo;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import org.sirmupadbapi.beans.*;
import org.gingresos.start.MySesion;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import org.sirmupadbapi.controller.RegimenController;
import org.sirmupadbapi.controller.SubgruporegimenController;
import org.sirmupadbapi.generales.Mensajeria;

/**
 *
 * @author avbravo
 */
public class RegimenListar extends javax.swing.JInternalFrame {

    DefaultTableModel modelo;
    int fila = -1;
    RegimenController regimenController1;
    Regimen regimen1;
    SubgruporegimenController subgruporegimenController1;
    Subgruporegimen subgruporegimen1;
    
    String nombreSubGrupoRegimen="";

    /** Creates new form GastoSegunObjetoListar */
    public RegimenListar() {
        initComponents();
        regimenController1 = new RegimenController();
        subgruporegimenController1 = new SubgruporegimenController();
         regimen1 = new  Regimen();
        subgruporegimen1 = new Subgruporegimen();
       
        modelo = (DefaultTableModel) jXTable1.getModel();
        if (subgruporegimenController1.selectAll()) {
            for (Subgruporegimen subgruporegimen : subgruporegimenController1.getSubgruporegimenlist()) {

                jXComboBoxSubGrupo.addItem(subgruporegimen.getIDSUBGRUPOREGIMEN() + " - " + subgruporegimen.getSUBGRUPO());
            }
        }

        AutoCompleteDecorator.decorate(this.jXComboBoxSubGrupo);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jXComboBoxSubGrupo = new org.jdesktop.swingx.JXComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();
        jButtonImprimir = new javax.swing.JButton();
        jButtonCerrar = new javax.swing.JButton();
        jButtonImprimirTodos = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Regimen Listar");

        jLabel2.setText("Sub Grupo");

        jXComboBoxSubGrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXComboBoxSubGrupoActionPerformed(evt);
            }
        });

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Regimen", "SubGrupo"
            }
        ));
        jScrollPane1.setViewportView(jXTable1);

        jButtonImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gingresos/resources/gif/print.png"))); // NOI18N
        jButtonImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirActionPerformed(evt);
            }
        });

        jButtonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gingresos/resources/gif/return.png"))); // NOI18N
        jButtonCerrar.setToolTipText("Cerrar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        jButtonImprimirTodos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gingresos/resources/gif/print.png"))); // NOI18N
        jButtonImprimirTodos.setText("Todos");
        jButtonImprimirTodos.setToolTipText("Imprimir todos");
        jButtonImprimirTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirTodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(200, 200, 200)
                        .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonImprimirTodos, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jXComboBoxSubGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 558, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 674, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(54, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jXComboBoxSubGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButtonImprimir, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                    .addComponent(jButtonImprimirTodos, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                    .addComponent(jButtonCerrar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        getAccessibleContext().setAccessibleName("Sub Grupo Regimen Listar");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jXComboBoxSubGrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXComboBoxSubGrupoActionPerformed
        // TODO add your handling code here:
        try {
            int j = jXComboBoxSubGrupo.getSelectedIndex();
            if (j != -1) {

////Remove all TableData
                //limpia el jtable
                if (modelo != null) {
                    while (modelo != null && modelo.getRowCount() > 0) {
                        modelo.removeRow(0);
                    }
                }


                subgruporegimen1 = subgruporegimenController1.getSubgruporegimenlist().get(j);
this.nombreSubGrupoRegimen = subgruporegimen1.getIDSUBGRUPOREGIMEN() +"-"+subgruporegimen1.getSUBGRUPO();
                Map<String, String> parametros = new HashMap<String, String>();
                parametros.put("idsubgruporegimen", subgruporegimen1.getIDSUBGRUPOREGIMEN());

                boolean encontrado = regimenController1.locateByParameters(parametros);


                if (!encontrado) {
                    // Mensajeria.Mensaje(" No existen registros para este grupo");
                } else {
                    Object[] filas = new Object[3]; // Hay tres columnas en la tabla
                    for (Regimen regimen:regimenController1.getRegimenlist()) {
                       

                        filas[0] = regimen.getIdregimen();
                        filas[1] = regimen.getRegimen();

                        filas[2] = regimen.getIdsubgruporegimen();
                        modelo.addRow(filas);

                    }
                }
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "Filtrar()");
        }
    }//GEN-LAST:event_jXComboBoxSubGrupoActionPerformed

private void jButtonImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirActionPerformed
// TODO add your handling code here:
    try {

        HashMap map = new HashMap();
        map.put("p_municipio", MySesion.getMunicipio());
        map.put("p_subgruporegimen",this.nombreSubGrupoRegimen);
      //  if (gastossegunobjetoController1.selectAll()) {
            Imprimir(regimenController1.getRegimenlist(), map,false);
       // }


    } catch (Exception ex) {
        Mensajeria.MensajeError(ex, "Imprimir()");
    }
}//GEN-LAST:event_jButtonImprimirActionPerformed

private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
// TODO add your handling code here:
    dispose();
}//GEN-LAST:event_jButtonCerrarActionPerformed

    private void jButtonImprimirTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirTodosActionPerformed
 try {

        HashMap map = new HashMap();
        map.put("p_municipio", MySesion.getMunicipio());
       /// map.put("p_subgruporegimen",this.nombreSubGrupoRegimen);
      //  if (gastossegunobjetoController1.selectAll()) {
       if(regimenController1.selectAll()){
         Imprimir(regimenController1.getRegimenlist(), map,true);  
       }
            
       // }


    } catch (Exception ex) {
        Mensajeria.MensajeError(ex, "Imprimir()");
    }
    }//GEN-LAST:event_jButtonImprimirTodosActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new GastoSegunObjetoListar().setVisible(true);
//            }
//        });
//    }

    private void Imprimir(List<Regimen> aux, HashMap map, boolean todos) throws JRException {
        try {

            JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(aux);
             InputStream reporte;
            if(todos){
                  reporte = (InputStream) getClass().getResourceAsStream("/org/gema/reportes/regimenimpositivo/RegimenTodos.jasper");
            }else{
                 reporte = (InputStream) getClass().getResourceAsStream("/org/gema/reportes/regimenimpositivo/Regimen.jasper");
            }
          
            if (reporte == null) {
                Mensajeria.Mensaje("No se pudo cargar el reporte");
            } else {

                JasperPrint print = JasperFillManager.fillReport(reporte, map, ds);
                JasperViewer.viewReport(print, false);

            }

        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "Imprimir()");
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonImprimir;
    private javax.swing.JButton jButtonImprimirTodos;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXComboBox jXComboBoxSubGrupo;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables
}
